﻿using BusinessService.Plugin.DataSource.Sqlite;
using BusinessService.Plugin.DataSource.Sqlite.Dtos;
using BusinessService.Plugin.Entity;
using BusinessService.Plugin.Helper;
using BusinessService.Plugin.ProtocolMethod;
using Elephant.FormUtils;
using EMSSevice;
using IFM.Core;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
namespace WindowsForm.Forms
{
    public partial class 发送关系设置 : Form
    {
        private SqliteLibrary _repSqlite => Program.IOC.GetRequiredService<SqliteLibrary>();
        ////条件筛选表的字段
        DataGridViewComboEditBoxColumn Column1 = new();
        DataGridViewComboEditBoxColumn Column2 = new();
        DataGridViewComboEditBoxColumn Column3 = new();
        DataGridViewButtonColumn buttonColumn = new();
        public 发送关系设置()
        {
            InitializeComponent();
        }
        private void button3_Click(object sender, EventArgs e)
        {
            //jsonView.AddRow();
        }
        private void JsonSrtuctSet_Load(object sender, EventArgs e)
        {
            var nameList = _repSqlite.sqliteQuery().Select(x => x.Name).ToList();
            if (Tag == null)  //新增打开操作
            {
                foreach (var item in nameList)
                {
                    uiTransfer1.ItemsLeft.Add(item);
                };
            }
            else  //编辑打开操作
            {
                var param = _repSqlite.JsonStructQuery((long)Tag);
                inputName.Text = param.Name;
                var temp = JsonUtil.ToObject<List<string>>(param.DataSourceList);
                UploadTarget.Text = param.UploadTarget;
                labelType.Text=param.UploadTargetType;
                if (!SqlFunc.IsNullOrEmpty(temp))
                {
                    foreach (var item in temp)
                    {
                        uiTransfer1.ItemsRight.Add(item);
                    };
                    var Except = nameList.Except(temp);
                    foreach (var item in Except)
                    {
                        uiTransfer1.ItemsLeft.Add(item);
                    }
                }
            }


            foreach (var item in _repSqlite.GetProtocolName())
            {
                UploadTarget.Items.Add(item);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (inputName.Text == string.Empty)
            {
                MessageBox.Show("请您编辑Json结构名称！", "信息提示");
                return;
            }

            JsonBuildEntity input = new JsonBuildEntity();
            input.Name = inputName.Text;
            input.CreateTime = DateTime.Now;
            input.DataSourceList = uiTransfer1.ItemsRight.ToJsonString();
            input.UploadTarget = UploadTarget.Text;
            input.UploadTargetType = labelType.Text;
            if (SqlFunc.IsNullOrEmpty(Tag))
            {
                input.ID = 0;
            }
            else
            {
                input.ID = (long)Tag;
            }
            _repSqlite.JsonEdit(input);

            //刷新表格数据
            if (JsonBuild.RefreshTable != null) JsonBuild.RefreshTable();
        }
        private void select1_SelectedIndexChanged(object sender, int value)
        {
            labelType.Text = _repSqlite.GetProtocolType(UploadTarget.Text);
        }

        private void labelType_Click(object sender, EventArgs e)
        {

        }
    }
}
